const fs = require('fs');
const execa = require('execa');

const targets = fs.readdirSync('packages').filter(f => {
  // 过滤掉文件
  if (!fs.statSync(`packages/${f}`).isDirectory()) {
    return false;
  }
  return true;
});

async function build(target) {
  // 执行命令rollup -c --environment TARGET:shared
  const env = 'production';
  await execa(
    'rollup',
    [
      '-c',
      '--environment',
      [
        `TARGET:${target}`,
        `NODE_ENV:${env}`,
      ].filter(Boolean).join(',')
    ],
    { stdio: 'inherit' }
  )
}

function runParallel(targets, iteratorFn) {
  const res = [];
  for (const item of targets) {
    const p = iteratorFn(item); // 返回 promise
    res.push(p);
  }

  return Promise.all(res);
}

runParallel(targets, build).then(() => {

});